package com.hframe.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport {
    private static final long serialVersionUID = 1L;
    private String href = "";
    private int page = 0;
    private int pageCount = 0;
    private String name = "li";
    private String prev = "Prev";
    private String next = "Next";
    private String empty = "...";
    private String replace = null;

    public String getReplace() {
        return replace;
    }

    public void setReplace(String replace) {
        this.replace = replace;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = 0 < page ? page : 1;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = 0 < pageCount ? pageCount : 1;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrev() {
        return prev;
    }

    public void setPrev(String prev) {
        this.prev = prev;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }

    public String getEmpty() {
        return empty;
    }

    public void setEmpty(String empty) {
        this.empty = empty;
    }

    @Override
    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            if (1 >= page || 0 == pageCount)
                out.print(createLink(0, prev, null));
            else
                out.print(createLink(0, prev, createHref(page - 1)));

            if (pageCount > 0) {
                int start = page - 2;
                if (start < 1)
                    start = 1;

                int end = start + 4;
                if (end > pageCount)
                    end = pageCount;

                if (end - start < 4)
                    start = end - 4;

                if (start < 1)
                    start = 1;

                if (start >= 2)
                    out.print(createLink(1, "1", createHref(1)));
                if (start > 2)
                    out.print(createLink(0, empty, null));

                for (int i = start; i <= end; i++) {
                    out.print(createLink(i, "" + i, createHref(i)));
                }

                if (pageCount - end > 1)
                    out.print(createLink(0, empty, null));
                if (pageCount - end >= 1)
                    out.print(createLink(pageCount, "" + pageCount, createHref(pageCount)));

            }

            if (pageCount <= page || 0 == pageCount)
                out.print(createLink(0, next, null));
            else
                out.print(createLink(0, next, createHref(page + 1)));

        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }

        return super.doEndTag();
    }

    private String createLink(int page, String text, String href) {

        if (null == href) {
            return "<" + name + " class=\"disabled\"><a>" + text + "</a></" + name + ">";

        } else if (this.page == page)
            return "<" + name + " class=\"active\"><a>" + text + "</a></" + name + ">";

        return "<" + name + "><a href =\"" + href + "\">" + text + "</a></" + name + ">";
    }

    private String createHref(int page) {
        if (0 == href.length())
            return "?page=" + page;

        else if (null != replace) {
            return href.replace(replace, "" + page);

        } else {
            int temp = href.indexOf("?");
            if (-1 == temp)
                return href + "?page=" + page;
            else if (href.length() == temp)
                return href + "page=" + page;
            else
                return href + "&page=" + page;
        }
    }
}
